<?php
class WebUtils {

  public static $mGenAutoload = "gen_autoload.php";
  
  
  public static function addDataObjectToGenAutoload($className, $path) {
    return self::addClassToGenAutoload($className, $path, "data_objects/"); // TODO: replace this with a constant
  }
  
  public static function addScaffolderToGenAutoload($className, $path) {
    return self::addClassToGenAutoload($className, $path, ScaffolderGenerator::SCAFFOLDER_SUBDIR.$className."/");
  }
  
  public static function addClassToGenAutoload($className, $path, $relClassPath) {
    $smarty = new SmartyExt();
    $smarty->right_delimiter = ":]";
    $smarty->left_delimiter = "//[:"; 
  
    $classRecord = "case '" . $className . "': require_once(\$myDir .'".$relClassPath.$className.".php'); break;\n";
    $classRecord .= "\t//[:\$newClassRecord:]";
  
    $oldGenAutoload = file_get_contents($path."config/".self::$mGenAutoload);
    // check if this data object has already been added:
    if (strpos($oldGenAutoload, $classRecord) > 0) die("Class $className has already been added\n");
  
    $smarty->assign("newClassRecord", $classRecord);
    $newGenAutoload = $smarty->fetch( $path."config/".self::$mGenAutoload);
    file_put_contents($path."config/".self::$mGenAutoload, $newGenAutoload);
  }
  
  
  public static function genNewDataObject($className, $path) {
    $myDir = dirname(__FILE__) . "/";
    $smarty = new SmartyExt();
    $smarty->right_delimiter = ":]";
    $smarty->left_delimiter = "//[:";
     
    $smarty->assign("className", $className);
    $newDataObject = $smarty->fetch($myDir. "data_object_tpl.dry");
    file_put_contents($path."data_objects/".$className. ".php", $newDataObject);
  }
  
  public static function removeClassFromGenAutoload($className, $path) {
    $oldGenAutoload = file_get_contents($path."config/".self::$mGenAutoload);
    $newGenAutoload = preg_replace( "/.*case *'".$className."' *:.*break;\n/", "", $oldGenAutoload );
    file_put_contents($path."config/".self::$mGenAutoload, $newGenAutoload);
  }
  
  public static function deleteDataObjectClass($className, $path) {
    unlink($path."data_objects/".$className.".php");
  }
  
  public static function deleteScaffolder($className, $path) {
    if ( !is_dir($path.ScaffolderGenerator::SCAFFOLDER_SUBDIR.$className) ) {
      echo $className. " at ". $path.ScaffolderGenerator::SCAFFOLDER_SUBDIR.$className . " doesn't exist\n";
      return;
    }
    
    $rmCmd = "rm -Rdf ". $path.ScaffolderGenerator::SCAFFOLDER_SUBDIR.$className;
    shell_exec($rmCmd);
  }
  
  public static function genControllerConfig(Array $contrConfig) {
    $conf = "";
    foreach($contrConfig as $contrName => $actionsArray) {
      $conf .= "'".$contrName . "' => ";
      if (is_array($actionsArray)) {
        $conf .= "array( ";
        foreach($actionsArray as $actName => $numArgs) {
          $conf .= "'".$actName."' => ". $numArgs.",";
        }
        $conf = substr($conf, 0 , -1). "),";
      }
       else {
         $conf .= ((is_null($actionsArray))?"NULL":$actionsArray).",";
       }   
    }
    $conf = substr($conf, 0, -1);
    $conf = "<?php\n Conf::set('controllerConfig',array(" . $conf . "));";
    return $conf;
  }
  
  /**
   * Returns content of modified conroller config
   */
  public static function addControllerToConfig($className) {
    $contrConfig = Conf::get('controllerConfig');
    $contrConfig[$className] = NULL;
    return self::genControllerConfig($contrConfig);    
  }
  
  
  public static function removeControllerFromConfig($className) {
    $arr = array();
    $contrConfig = Conf::get('controllerConfig');
    foreach($contrConfig as $contrName => $actions ) {
      if ($contrName != $className ) $arr[$contrName] = $actions;
    }
    return self::genControllerConfig($arr);   
  }
  
  
  public static function addActionToConfig($contrName, $actionName, $numArgs) {
    $contrConfig = Conf::get('controllerConfig');
    if (array_key_exists($contrName, $contrConfig) == false) {
      throw new WebUtilsException("Can't add action to controller ". $contrName . " as this controller doesn't exist");
    }
    
    if ( !is_null($contrConfig[$contrName]) && array_key_exists($actionName, $contrConfig[$contrName]) ) {
      throw new WebUtilsException("Can't add action ". $actionName . " to controller ". $contrName ." action with this name already exists");
    }
    
    if ( is_null($contrConfig[$contrName]) ) $contrConfig[$contrName] = array($actionName=>$numArgs);
     else  $contrConfig[$contrName][$actionName] = $numArgs;
     
    return self::genControllerConfig($contrConfig);    
  }
  
  
  public static function removeActionFromConfig($contrName, $actionName) {
    $contrConfig = Conf::get('controllerConfig');
    if (array_key_exists($contrName, $contrConfig) == false) {
      throw new WebUtilsException("Can't remove action from controller ". $contrName . " as this controller doesn't exist");
    }
    
    if ( is_null($contrConfig[$contrName]) || array_key_exists($actionName, $contrConfig[$contrName]) == false ) {
      throw new WebUtilsException("Can't remove action ". $actionName . " from controller ". $contrName ." this action doesn't exist");
    }
    
    $arr = array();
    foreach($contrConfig as $contr => $acts ) {
      if ($contr != $contrName) {
         $arr[$contr] = $acts;
        continue;
      }
      $arr[$contr] = NULL;
      foreach( $acts as $an => $num ) 
        if ($an != $actionName ) $arr[$contr][$an] = $num;
    }
    return self::genControllerConfig($arr);
  }
}

class WebUtilsException extends Exception {}
